package com.wsh.controller.generator;

import com.wsh.entity.generator.TableColumnInfoEntity;
import com.wsh.exception.BadRequestException;
import com.wsh.generator.TableColumnInfoService;
import com.wsh.utils.JsonResult;
import com.wsh.vo.generator.TableColumnInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/tableInfo")
public class TableColumnInfoController {
    @Autowired
    private TableColumnInfoService tableColumnInfoService;

    /**
     * 跟新表信息
     *
     * @return
     */
    @PostMapping("/updateTable")
    public Object updateTableColumnInfo(@RequestBody TableColumnInfoVo dto) {
        try {
            tableColumnInfoService.updateTableColumnInfo(dto);
        } catch (BadRequestException b) {
            log.error("TableColumnInfoController =>>> updateTableColumnInfo Exception={}", b.getMessage());
            return JsonResult.fail("配置错误，" + b.getMessage());
        } catch (Exception e) {
            log.error("TableColumnInfoController =>>> updateTableColumnInfo 其他错误 Exception={}", e.getMessage());
            return JsonResult.fail("系统错误，请联系管理员！");
        }
        return JsonResult.success(true);
    }


    /**
     * 查询表属性信息
     *
     * @return
     */
    @GetMapping("/columnList")
    public Object columnList(String tableName) {
        List<TableColumnInfoEntity> list = null;
        try {
            list = tableColumnInfoService.columnList(tableName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonResult.success(list);
    }

}
